package com.liunian.subject.domain.handler.subject;

import com.liunian.subject.common.enums.SubjectTypeEnum;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class SubjectInfoTypeFactory implements InitializingBean {

    @Resource
    private List<SubjectInfoTypeHandler> handlerList;

    private static final Map<SubjectTypeEnum, SubjectInfoTypeHandler> HANDLERS = new HashMap<>();

    public SubjectInfoTypeHandler getHandler(int type) {
        return HANDLERS.get(SubjectTypeEnum.getByCode(type));
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        for (SubjectInfoTypeHandler handler : handlerList) {
            HANDLERS.put(handler.getSubjectType(), handler);
        }
    }
}
